【笔试题】用PHP写一个微波炉
代码题(用 OOP 的思想编码,注意代码规范) 写出你的思路,最好有代码
用 php 写一个微波炉,注意物品正加热时不能开门,带皮带壳食物不能被加热。
感谢大佬们的建议,更进一步理解了 OOP,现更新第二版。
第二版
<?php /** * Created by PhpStorm. * User: arun * Date: 2019-04-30 * Time: 16:10 */ /** * 厨房用具 * Interface kitchenWare */ interface kitchenWare { /** * 加工食材 * @param Food $food * @return mixed */ public function process(Food $food); /** * 是否正在加工 * @return mixed */ public function hasProcess(); } /** * 微波炉 * Class MicrowaveOven */ class MicrowaveOven implements kitchenWare { /** * 是否加热中 * @var bool */ protected $is_heat = false; /** * @param Food $food * @return mixed|string */ public function process(Food $food) { if ($this->hasProcess()) { return '已有食物在加热,无法打开'; } else { if ($food->hasShuck() || $food->hasPericarp()) { return '食物带壳或者带皮,无法进行加热'; } else { $this->is_heat = true; return '食物加热中,加热完成即可取出'; } } } /** * 是否正在加工 * @return bool|mixed */ public function hasProcess() { return $this->is_heat; } } /** * 烤箱 * Class Oven */ class Oven implements kitchenWare { /** * 是否烧烤中 * @var bool */ protected $is_heat = false; /** * @param Food $food * @return mixed|string */ public function process(Food $food) { if ($this->is_heat) { return '已有食物在烤制,无法打开'; } else { if ($food->hasShuck()) { return '食物带壳,无法进行烤制'; } else { $this->is_heat = true; return '食物烤制中,完成即可取出'; } } } /** * 是否正在加工 * @return bool|mixed */ public function hasProcess() { return $this->is_heat; } } /** * 食物 * Class Food */ class Food { /** * 是否带壳 * @var bool */ protected $is_shuck = false; /** * 是否带皮 * @var bool */ protected $is_pericarp = false; /** * Food constructor. * @param bool $is_shuck * @param bool $is_pericarp */ public function __construct(bool $is_shuck, bool $is_pericarp) { $this->is_shuck = $is_shuck; $this->is_pericarp = $is_pericarp; } /** * 判断是否带壳 * @return bool */ public function hasShuck() { return $this->is_shuck; } /** * 判断是否带皮 * @return bool */ public function hasPericarp() { return $this->is_pericarp; } } /** * 烹饪 * Class Cooking */ class Cooking { /** * @var kitchenWare */ protected $kitchenWare; /** * Cook constructor. * @param kitchenWare $kitchenWare */ public function __construct(kitchenWare $kitchenWare) { $this->kitchenWare = $kitchenWare; } /** * 烹饪食物 * @param Food $food * @return mixed */ public function cooking(Food $food) { $data = $this->kitchenWare->process($food); return $data; } } /** * 微波炉加热 * @return mixed */ function test() { $cooking = new Cooking(new MicrowaveOven()); $food = new Food(false, false); $result = $cooking->cooking($food); $result2 = $cooking->cooking($food); var_dump($result, $result2); } /** * 烤箱烤制 * @return mixed */ function test2() { $cooking = new Cooking(new Oven()); $food = new Food(false, true); $result = $cooking->cooking($food); $result2 = $cooking->cooking($food); var_dump($result, $result2); } test(); test2();第一版
<?php /** * Created by PhpStorm. * User: arun * Date: 2019-04-30 * Time: 16:10 */ /** * 厨房用具 * Interface kitchenWare */ interface kitchenWare { /** * 加工食材 * * @param $food * @return mixed */ public function process($food); } /** * 微波炉 * Class MicrowaveOven */ class MicrowaveOven implements kitchenWare { /** * 是否加热中 * @var bool */ protected $is_heat = false; public function process($food) { if ($this->is_heat) { return '已有食物在加热,无法打开'; } else { if ($food['is_shuck'] || $food['is_pericarp']) { return '食物带壳或者带皮,无法进行加热'; } else { $this ->is_heat = true; return '食物加热中,加热完成即可取出'; } } } } /** * 烤箱 * Class Oven */ class Oven implements kitchenWare { /** * 是否烧烤中 * @var bool */ protected $is_heat = false; public function process($food) { if ($this->is_heat) { return '已有食物在烤制,无法打开'; } else { if ($food['is_shuck']) { return '食物带壳,无法进行烤制'; } else { $this ->is_heat = true; return '食物烤制中,完成即可取出'; } } } } /** * 食物 * Class Food */ class Food { /** * 是否带壳 * @var bool */ protected $is_shuck = false; /** * 是否带皮 * @var bool */ protected $is_pericarp = false; /** * Food constructor. * @param bool $is_shuck * @param bool $is_pericarp */ public function __construct(bool $is_shuck, bool $is_pericarp) { $this->is_shuck = $is_shuck; $this->is_pericarp = $is_pericarp; } public function getFood() { return [ 'is_shuck' => $this->is_shuck, 'is_pericarp' => $this->is_pericarp, ]; } } /** * 烹饪 * Class Cooking */ class Cooking { /** * @var kitchenWare */ protected $kitchenWare; /** * Cook constructor. * @param kitchenWare $kitchenWare */ public function __construct(kitchenWare $kitchenWare) { $this->kitchenWare = $kitchenWare; } /** * 烹饪食物 * @param $food * @return mixed */ public function cooking($food) { $data = $this->kitchenWare->process($food); return $data; } } /** * 微波炉加热 * @return mixed */ function test() { $cooking = new Cooking(new MicrowaveOven()); $food = new Food(false, false); $result = $cooking->cooking($food->getFood()); $result2 = $cooking->cooking($food->getFood()); var_dump($result, $result2); } /** * 烤箱烤制 * @return mixed */ function test2() { $cooking = new Cooking(new Oven()); $food = new Food(false, true); $result = $cooking->cooking($food->getFood()); $result2 = $cooking->cooking($food->getFood()); var_dump($result, $result2); } test(); test2();以上就是【笔试题】用PHP写一个微波炉的详细内容,更多请关注jquery中文网其它相关文章!
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/shell/11875.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Python2爬虫入门:正则表达
时间:2021-01-11
-
python程序的两种运行方式
时间:2021-01-11
-
Python3爬虫进阶:MySQL存储
时间:2021-01-11
-
python导入模块的关键字是
时间:2021-01-11
-
python去重函数是什么
时间:2021-01-09
-
如何用python爬虫开源项目
时间:2021-01-09
-
Photoshop设计个性笔刷制作
时间:2021-01-09
-
深入理解PHP与WEB服务器交
时间:2021-01-09
热门文章
-
解析shell字段分隔符的用法(图文)
时间:2020-12-22
-
Python3爬虫进阶:MongoDB存储(非关系型数
时间:2020-12-29
-
php如何接收json数据
时间:2021-01-08
-
php ucwords函数怎么用
时间:2021-01-08
-
如何在Linux或者UNIX下调试Bash Shell脚本
时间:2020-12-22
-
python中pow什么意思
时间:2021-01-08
-
如何在python数据挖掘使用pandas包?
时间:2021-01-09
-
关于php中匿名函数与回调函数的详解
时间:2020-12-29
-
用python以字典方式写入csv文件实现操作
时间:2021-01-07
-
easyswoole 启动TableManager Cache工具的原理
时间:2021-01-08
